import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: AnimatedSlideExample2());
  }
}

class AnimatedSlideExample2 extends StatefulWidget {
  @override
  _AnimatedSlideExample2State createState() => _AnimatedSlideExample2State();
}

class _AnimatedSlideExample2State extends State<AnimatedSlideExample2>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<Offset> _animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 3),
    );

    _animation =
        Tween<Offset>(
          begin: Offset.zero,
          end: const Offset(0.5, 0.5), // 向右下移动50%
        ).animate(
          CurvedAnimation(
            parent: _animationController,
            curve: Curves.elasticOut, // 弹性效果
          ),
        );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('平移 示例')),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _animationController.forward(from: 0);
        },
      ),
      body: SizedBox.expand(
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
              child: Container(color: Colors.black, width: 1),
              top: 0,
              bottom: 0,
              left: 200,
            ),
            Positioned(
              child: Container(color: Colors.black, height: 1),
              left: 0,
              right: 0,
              top: 200,
            ),
            Positioned(
              top: 200,
              left: 200,
              child: Column(
                children: [
                  SlideTransition(
                    position: _animation,
                    child: GestureDetector(
                      child: Container(
                        width: 200,
                        height: 100,
                        color: Colors.blue.withValues(alpha: 0.8),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
